package com.ourteam.product.api;

import java.util.Date;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.ProductUITreeDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITreeNodeDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUITreeBean;
import com.ourteam.product.ui.domain.ProductUITreeNodeBean;
import com.ourteam.product.ui.domain.ProductUITreeNodeBuilderBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.model.tree.NodeBean;
import net.ui.model.tree.SubNodeBuilderBean;
import net.ui.model.tree.TreeBean;
import net.ui.model.tree.TreeNodeBean;

@Controller
@RequestMapping("/api/PRODUCT/UI/productUITree")
public class ProductUITreeAPI
		extends AbstractCRUDServiceController<ProductUITreeDAOQueryBean, ProductUITreeSearchModel, ProductUITreeBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUITreeDAOQueryBean newQueryBean() {
		return new ProductUITreeDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUITreeDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUITreeCount(queryBean);
	}

	@Override
	protected ProductUITreeBean[] queryData(ProductUITreeDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUITrees(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUITrees(ids);
	}

	@Override
	protected ProductUITreeBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUITreeById(id);
	}

	@Override
	protected void saveData(ProductUITreeBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUITree(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUITree(dataBean);
		}

	}

	@RequestMapping("/getTree")
	@ResponseBody
	public TreeBean getTree(@RequestParam long id) throws Exception {

		ProductUITreeBean productUITreeBean = PRODUCT_UI_SERVICE.getProductUITreeById(id);

		TreeBean treeBean = new TreeBean();
		
		ProductUITreeNodeDAOQueryBean queryBean = new ProductUITreeNodeDAOQueryBean();

		queryBean.createCriteria().andTreeIdEqualTo(id);

		ProductUITreeNodeBean[] treeNodeBeans = PRODUCT_UI_SERVICE.queryProductUITreeNodes(queryBean);

		for (int i = 0; i < treeNodeBeans.length; i++) {

			ProductUITreeNodeBean productUITreeNodeBean = treeNodeBeans[i];

			NodeBean nodeBean = new NodeBean();

			nodeBean.setDesc(productUITreeNodeBean.getRemarks());

			nodeBean.setId(String.valueOf(productUITreeNodeBean.getId()));

			nodeBean.setImage(productUITreeNodeBean.getIcon());

			nodeBean.setLabel(productUITreeNodeBean.getTitle());

			nodeBean.setLabelKey(productUITreeNodeBean.getTitleKey());

			nodeBean.setLeaf(BooleanUtils.toBoolean(productUITreeNodeBean.getIsLeaf()));

			ProductUITreeNodeBuilderBean[] nodeBuilderBeans = PRODUCT_UI_SERVICE
					.getProductUITreeNodeBuilders(productUITreeNodeBean.getId());

			for (int j = 0; j < nodeBuilderBeans.length; j++) {
				ProductUITreeNodeBuilderBean productUITreeNodeBuilderBean = nodeBuilderBeans[j];

				SubNodeBuilderBean subNodeBuilderBean = new SubNodeBuilderBean();

				subNodeBuilderBean.setId(productUITreeNodeBuilderBean.getCode());

				subNodeBuilderBean.setDesc(productUITreeNodeBuilderBean.getRemarks());

				subNodeBuilderBean.setClassName(productUITreeNodeBuilderBean.getNodeBuilder());

				nodeBean.addSubNodeBuilder(subNodeBuilderBean);
			}

			treeBean.addNodeDefine(nodeBean);
		}

		ProductUITreeNodeBean[] rootNodeBeans = PRODUCT_UI_SERVICE.getProductUITreeRootNodes(id, 0);

		for (int i = 0; i < rootNodeBeans.length; i++) {

			ProductUITreeNodeBean productUITreeNodeBean = rootNodeBeans[i];

			TreeNodeBean rootNode = new TreeNodeBean();

			rootNode.setId(String.valueOf(productUITreeNodeBean.getId()));

			loadSubRootTreeNode(rootNode, productUITreeNodeBean);

			treeBean.addRootNode(rootNode);

		}

		return treeBean;
	}

	protected void loadSubRootTreeNode(TreeNodeBean rootNode, ProductUITreeNodeBean treeNodeBean) throws Exception {

		ProductUITreeNodeBean[] rootNodeBeans = PRODUCT_UI_SERVICE.getProductUITreeRootNodes(treeNodeBean.getTreeId(),
				treeNodeBean.getId());

		for (int i = 0; i < rootNodeBeans.length; i++) {
			ProductUITreeNodeBean productUITreeNodeBean = rootNodeBeans[i];

			TreeNodeBean subNode = new TreeNodeBean();

			subNode.setId(String.valueOf(productUITreeNodeBean.getId()));

			loadSubRootTreeNode(subNode, productUITreeNodeBean);

			rootNode.addSubNode(subNode);

			loadSubRootTreeNode(subNode, productUITreeNodeBean);
		}

	}

}
